//
//  EXRemoteKVInvoker.m
//  CommonSpaces
//
//  Created by PH on 9.2.10.
//  Copyright 2010 Codesign. All rights reserved.
//

#import "EXRemoteKVInvoker.h"

@implementation EXRemoteKVInvoker

- (id)initWithObject:(id)object withObject:(id)_object2 {
	if ((self = [super init])) {
		object2 = [_object2 retain];
		pointer = [[NSValue valueWithPointer: object] retain];
	}
	return self;
}

- (id)initWithPath:(NSString*)path {
	NSAssert(NO, @"Invalid method invocation on remote key-value store (initWithPath)");
	return nil;
}

- (BOOL)isReady {
	return [(id) [pointer pointerValue] isReady];
}

- (BOOL)putMap:(NSDictionary*)dict forKey:(NSString*)key {
	return [(id) [pointer pointerValue] putMap: dict forKey: key];
}

- (NSArray*)getMapsWithCriteria:(NSDictionary*)criteria {
	return [(id) [pointer pointerValue] getMapsWithCriteria: criteria];
}

- (NSArray*)getMapsWithCriteria:(NSDictionary*)criteria condBlock:(void(^)(void*))condBlock {
	return [(id) [pointer pointerValue] getMapsWithCriteria: criteria condBlock: condBlock];
}

- (NSDictionary*)getMapForKey:(NSString*)key {
	return [(id) [pointer pointerValue] getMapForKey: key];
}

- (BOOL)removeMapForKey:(NSString*)key {
	return [(id) [pointer pointerValue] removeMapForKey: key];
}

- (BOOL)removeRecordsMatchingCriteria:(NSDictionary*)criteria {
	return [(id) [pointer pointerValue] removeRecordsMatchingCriteria: criteria];
}

- (BOOL)createIndexOnField:(NSString*)fieldName rebuild:(BOOL)rebuild {
	return [(id) [pointer pointerValue] createIndexOnField: fieldName rebuild: rebuild];
}

- (BOOL)beginTransaction { // we need to ensure that the transaction lock will be accessed in the same thread
	BOOL retVal = [(id) [pointer pointerValue] beginTransaction];
	if (retVal) lockThread = [NSThread currentThread];
	return retVal;
}

- (BOOL)commitTransaction { // we need to ensure that the transaction lock will be accessed in the same thread
	@try {
		[(id) [pointer pointerValue] performSelector: @selector(commitTransaction) onThread: lockThread withObject: nil waitUntilDone: YES];
		return YES;
	} @catch (NSException* exception) {
		NSLog(@"Commit transaction: %@", exception);
	}
	return NO;
}

- (BOOL)rollbackTransaction { // we need to ensure that the transaction lock will be accessed in the same thread
	@try {
		[(id) [pointer pointerValue] performSelector: @selector(rollbackTransaction) onThread: lockThread withObject: nil waitUntilDone: YES];
		return YES;
	} @catch (NSException* exception) {
		NSLog(@"Rollback transaction: %@", exception);
	}
	return NO;
}

- (NSString*)uniqueKey {
	return [(id) [pointer pointerValue] uniqueKey];
}

- (NSString*)lastErrorString {
	return [(id) [pointer pointerValue] lastErrorString];
}

- (void(^)(void*))stringEqualConditionWithColumnName:(NSString*)columnName value:(NSString*)value {
	return [(id) [pointer pointerValue] stringEqualConditionWithColumnName: columnName value: value];
}

- (void(^)(void*))stringIncludedConditionWithColumnName:(NSString*)columnName value:(NSString*)value {
	return [(id) [pointer pointerValue] stringIncludedConditionWithColumnName: columnName value: value];
}

- (void(^)(void*))numberLessThanConditionWithColumnName:(NSString*)columnName value:(double)value {
	return [(id) [pointer pointerValue] numberLessThanConditionWithColumnName: columnName value: value];
}

- (void(^)(void*))numberGreaterThanConditionWithColumnName:(NSString*)columnName value:(double)value {
	return [(id) [pointer pointerValue] numberGreaterThanConditionWithColumnName: columnName value: value];
}

- (void(^)(void*))numberEqualConditionWithColumnName:(NSString*)columnName value:(double)value {
	return [(id) [pointer pointerValue] numberEqualConditionWithColumnName: columnName value: value];
}

- (void)close {
	//NSLog(@"Invalid method invocation on remote key-value store (close) -- ignoring");
}

- (void)dealloc {
	[pointer release];
	[object2 release];
	[super dealloc];
}

@end
